package com.apache.info.util;

import com.apache.uct.common.ToolsUtil;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
 * 对文件的删除、剪切、移动和创建文件夹操作
 *
 * @author Hou Dayu
 */
public class FileOperate {

    private static Logger log = LoggerFactory.getLogger(FileOperate.class);

    private static FileOperate instance = null;

    private FileOperate() {

    }

    public synchronized static FileOperate getInstance() {
        if (null == instance)
            instance = new FileOperate();
        return instance;
    }

    /**
     * 检查文件或文件夹是否存在
     *
     * @param folderPath 文件或文件夹路径
     * @return true为存在，false不存在
     */
    public boolean checkFolder(String folderPath) {
        boolean result = false;
        if (ToolsUtil.isNotNull(folderPath)) {
            File newFilePath = new File(folderPath);
            result = newFilePath.exists();
        }
        return result;
    }

    /**
     * 新建目录操作
     *
     * @param folderPath 文件夹路径
     */
    public boolean newCreateFolder(String folderPath) {
        boolean result = false;
        try {
            if (ToolsUtil.isNotNull(folderPath)) {
                File newFilePath = new File(folderPath);
                if (!newFilePath.exists()) {
                    result = newFilePath.mkdirs();
                }
            }
        } catch (Exception e) {
            log.error("新建目录操作出错ERROR KEY:" + e.getMessage());
        }
        return result;
    }

    /**
     * 创建新的文件
     *
     * @param filePathAndName 文件名称
     * @param fileContent 文件内容
     */
    public boolean newCreateFile(String filePathAndName, String fileContent) {
        boolean result = false;
        FileOutputStream fo = null;
        OutputStreamWriter osw = null;
        PrintWriter out1 = null;
        try {
            if (ToolsUtil.isNotNull(filePathAndName)) {
                File newFilePath = new File(filePathAndName);
                if (!newFilePath.exists()) {
                    newFilePath.createNewFile();
                }
                fo = new FileOutputStream(filePathAndName);
                osw = new OutputStreamWriter(fo, "UTF-8");
                out1 = new PrintWriter(osw);
                out1.println(fileContent);
                result = true;
            }
        } catch (Exception e) {
            log.error("新建文件操作出错ERROR KEY:" + e.getMessage());
        } finally {
            IOUtils.closeQuietly(out1);
            IOUtils.closeQuietly(osw);
            IOUtils.closeQuietly(fo);
        }
        return result;
    }

    /**
     * description: 获取指定文件夹下的文件列表
     *
     * @param filePath 文件夹路径
     * @return
     */
    public File[] getFileList(String filePath) {
        File file = new File(filePath);
        File flist[] = file.listFiles();
        return flist;
    }

    /**
     * 返回文件名
     *
     * @param fileName
     * @return
     */
    public String getFileName(String fileName) {
        File file = new File(fileName);
        return file.getName();
    }

    /**
     * 格式化文件路径
     *
     * @param path 文件路径
     * @return
     */
    public String formatFilePath(String path) {
        if (ToolsUtil.isNull(path))
            return "";
        path = path.replace("\\", "/");
        path = path.replace("\\\\", "/");
        return path.replace("//", "/");
    }

    /**
     * 首字母小写
     *
     * @param str
     * @return
     */
    public String toLowerFirst(String str) {
        if (ToolsUtil.isNull(str))
            return "";
        StringBuilder sb = new StringBuilder();
        sb.append(str.substring(0, 1).toLowerCase() + str.substring(1));
        return sb.toString();
    }

    /**
     * 首字母大写
     *
     * @param str
     * @return
     */
    public String toUpperFirst(String str) {
        if (ToolsUtil.isNull(str))
            return "";
        String names[] = str.split("_");
        StringBuilder sb = new StringBuilder();
        if (names.length < 2) {
            sb.append(names[0].substring(0, 1).toUpperCase() + names[0].substring(1));
        } else {
            for (int i = 0; i < names.length; i++) {
                sb.append(names[i].substring(0, 1).toUpperCase() + names[i].substring(1));
            }
        }
        return sb.toString();
    }

    /**
     * 属性值生成,符合骆驼命名
     *
     * @param str
     * @return
     */
    public String toCamelNamed(String str) {
        if (ToolsUtil.isNull(str))
            return "";
        String names[] = str.split("_");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < names.length; i++) {
            if (i == 0) {
                sb.append(names[i].toLowerCase());
            } else {
                sb.append(names[i].substring(0, 1).toUpperCase() + names[i].substring(1)
                        .toLowerCase());
            }
        }
        return sb.toString();
    }

}
